/**
 * 定时任务日志管理初始化
 */
var JobLog = {
    id: "JobLogTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
JobLog.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '任务日志id', field: 'id', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '任务id', field: 'jobId', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: 'spring bean名称', field: 'beanName', align: 'center', valign: 'middle', sortable: true},
        {title: '方法名', field: 'methodName', align: 'center', valign: 'middle', sortable: true},
        {title: '参数', field: 'params', align: 'center', valign: 'middle', sortable: true},
        {title: '任务状态', field: 'status', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value==1){
                return "<span class='label label-warning'>失败</span>";
        	}else{
                return "<span class='label label-success'>成功</span>";
        	}
        }},
        {title: '失败信息', field: 'error', align: 'center', valign: 'middle', sortable: true},
        {title: '耗时(单位：毫秒)', field: 'times', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true},
    ];
};

/**
 * 检查是否选中
 */
JobLog.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        JobLog.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加定时任务日志
 */
JobLog.openAddJobLog = function () {
    var index = layer.open({
        type: 2,
        title: '添加定时任务日志',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/joblog/to_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看定时任务日志详情
 */
JobLog.openJobLogDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '定时任务日志详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Sys.backendPath + '/joblog/to_update/' + JobLog.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除定时任务日志
 */
JobLog.del = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/joblog/delete", function (data) {
	            Sys.success("删除成功!");
	            JobLog.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("jobLogId",JobLog.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除定时任务日志?",operation);
    }
};
/**
 * 清空定时任务日志
 */
JobLog.deleteAll = function () {
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/joblog/delete_all", function (data) {
            Sys.success("清空成功!");
            JobLog.table.refresh();
        }, function (data) {
            Sys.error("清空失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否清空定时任务日志?",operation);
};

/**
 * 查询定时任务日志列表
 */
JobLog.search = function () {
    var queryData = {};
    queryData['jobId'] = $("\#jobId").val();
    queryData['beanName'] = $("\#beanName").val();
    queryData['methodName'] = $("\#methodName").val();
    queryData['params'] = $("\#params").val();
    queryData['status'] = $("\#status").val();
    queryData['error'] = $("\#error").val();
    queryData['times'] = $("\#times").val();
    JobLog.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = JobLog.initColumn();
    var table = new BSTable(JobLog.id, Sys.backendPath + "/joblog/list", defaultColunms);
    table.setPaginationType("server");
    table.setSort("createDate");
    JobLog.table = table.init();
});
